import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: '/login', component: () =>  import('@/views/login/LoginPage.vue') } ,
    {
      path: '/', component: () =>  import('@/views/layout/LayoutContainer.vue') ,
      redirect: '/article/manager',
      children: [{
        path: '/article/manager', component: () =>  import('@/views/article/ArticleManager.vue') 
      }, {
        path: '/article/channel', component: () =>  import('@/views/article/ArticleChannel.vue') 
      },
      { path: '/user/avatar', component: () =>  import('@/views/user/UserAvatar.vue') } ,
      {
        path: '/user/password', component: () => import('@/views/user/UserPassword.vue') 
      },
      { path: '/user/profile', component: () =>  import('@/views/user/UserProfile.vue') } ]
    }

  ],
})
router.beforeEach((to, from) => {
  const userStore=useUserStore()
  if(!userStore.token&&to.path!=='/login'){
    return '/login'
  }
  return true
})
// // 添加导航守卫，检查用户是否已登录
// router.beforeEach((to, from, next) => {
//   // 假设通过 localStorage 存储用户的登录状态
//   const isAuthenticated = !!localStorage.getItem('token');

//   // 如果用户未登录且目标路由不是登录页，则跳转到登录页
//   if (!isAuthenticated && to.path !== '/login') {
//     next('/login');
//   } else {
//     next();
//   }
// });

export default router

